import {
  getRegionalChartsData,
  getContrastChartsData,
  getFrequentSummary,
  getFrequentActivities,
  getG2AntvFrequentTable,
  getFrequencyDistributions,
  getPeopleNumberLineCharts,
  getSexPieData,
  getFrequencyAnalysisPieData,
  getPeopleNumberData,
  getThisYearHolidayList,
  getFormerHolidayList,
  getHolidayChartsData,
  getCaptureRecordsData
} from "@/api/peopleNumber"
import * as types from '../../mutation-types'
import { Message } from 'element-ui'
export default {
  namespaced: true, // 封闭
  state: {
    contrastOptions: {},
    lastTimeOptions: {},
    inRecentMonthOptions: {},
    optionsFrequencyDistributions: {},
    peopleNumberAnalyzeOptions: {},
    sexOptions: {},
    frequencyAnalysisOptions: {},
    holidayOptions: {},
    regionalChartsOptions:{},
  },
  mutations: {
    /**
     * 客流分析查询
     */
    // 客流分析展示图
    [types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS](state, options) {
      state.peopleNumberAnalyzeOptions = options
    },
    // 男女比例环形图
    [types.SET_SEX_DATA](state, options) {
      state.sexOptions = options
    },
    // 回头客分析环形图
    [types.SET_FREQUENCY_ANALYSIS_DATA](state, options) {
      state.frequencyAnalysisOptions = options
    },

    /**
     * 时段客流分析对比
     */
    // 对比图数据
    [types.SET_CONTRAST_CHARTS_DATA](state, options) {
      state.contrastOptions = options
    },

    [types.SET_REG_CHARTS_DATA](state, options) {
      state.regionalChartsOptions = options
    },

    /**
     * 回头客分析
     */
    // 回访活跃度分析 上次到访间隔天数分布
    [types.SET_LAST_TIME_DATA](state, options) {
      state.lastTimeOptions = options
    },
    // 近一月到访天数分布
    [types.SET_IN_RECENT_MONTH_DATA](state, options) {
      state.inRecentMonthOptions = options
    },
    // 回头客/新客分布走势分析
    [types.SET_FREQUENCY_DISTRIBUTIONS_DATA](state, options) {
      state.optionsFrequencyDistributions = options
    },

    // 节假日客流对比
    [types.SET_HOLIDAY_CHARTS_DATA](state, options) {
      state.holidayOptions = options
    }
  },
  getters: {},
  actions: {
    /**
     * 客流分析查询
     */
    // 客流分析展示图
    async fetchPeopleNumberLineCharts(context, payload) {
      let { type, checkList, startTime, endTime } = payload
      try {
        const { data } = await getPeopleNumberLineCharts(type, checkList, startTime, endTime)
        let name = '' // 1. 去重客流 2.销售额(元) 3.订单数 4.客单价(元)
        let nameArray = [] // 1. 去重客流 2.销售额(元) 3.订单数 4.客单价(元)
        if (checkList.length === 1) {
          if(checkList[0] === '1'){
            name = '去重客流'
          } else if (checkList[0] === '2'){
            name = '销售额(元)'
          } else if (checkList[0] === '3') {
            name = '订单数'
          } else if (checkList[0] === '4') {
            name = '客单价(元)'
          }
        } else if (checkList.length === 2){
          if (checkList[0] === '1') {
            if(checkList[1] === '2'){
              nameArray = ['去重客流', '销售额(元)']
            } else if (checkList[1] === '3') {
              nameArray = ['去重客流', '订单数']
            } else if (checkList[1] === '4') {
              nameArray = ['去重客流', '客单价(元)']
            }
          } else if (checkList[0] === '2') {
            if (checkList[1] === '3') {
              nameArray = ['销售额(元)', '订单数']
            } else if (checkList[1] === '4') {
              nameArray = ['销售额(元)', '客单价(元)']
            }
          } else if (checkList[0] === '3') {
            if (checkList[1] === '4') {
              nameArray = ['订单数', '客单价(元)']
            }
          }
        }
        if (type === 'hour') {
          if (checkList.length === 1){
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#2ec7c9', '#f4516c'],
              legend: {
                data: [name],
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[1]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: name,
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }else if(checkList.length === 2){
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#f4516c', '#2ec7c9'],
              legend: {
                data: nameArray,
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[1]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: [
                {
                  type: 'value',
                  // name: '销售额'
                },
                {
                  type: 'value',
                  // name: '客流量'
                }
              ],
              series: [
                {
                  name: nameArray[0],
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                },
                {
                  name: nameArray[1],
                  type: 'line',
                  yAxisIndex: 1,
                  data: (() => {
                    if (checkList[1] === '2' || checkList[1] === '4'){
                      return data.map(item => item.amount2 /100)
                    }else{
                      return data.map(item => item.amount2)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }
        } else if (type === 'day') {
          if (checkList.length === 1) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#2ec7c9', '#f4516c'],
              legend: {
                data: [name],
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: name,
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          } else if (checkList.length === 2) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#f4516c', '#2ec7c9'],
              legend: {
                data: nameArray,
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: [
                {
                  type: 'value',
                  // name: '销售额'
                },
                {
                  type: 'value',
                  // name: '客流量'
                }
              ],
              series: [
                {
                  name: nameArray[0],
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                },
                {
                  name: nameArray[1],
                  type: 'line',
                  yAxisIndex: 1,
                  data: (() => {
                    if (checkList[1] === '2' || checkList[1] === '4') {
                      return data.map(item => item.amount2 / 100)
                    } else {
                      return data.map(item => item.amount2)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }
        } else if (type === 'week') {
          if (checkList.length === 1) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#2ec7c9', '#f4516c'],
              legend: {
                data: [name],
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: name,
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          } else if (checkList.length === 2) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#f4516c', '#2ec7c9'],
              legend: {
                data: nameArray,
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: [
                {
                  type: 'value',
                  // name: '销售额'
                },
                {
                  type: 'value',
                  // name: '客流量'
                }
              ],
              series: [
                {
                  name: nameArray[0],
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                },
                {
                  name: nameArray[1],
                  type: 'line',
                  yAxisIndex: 1,
                  data: (() => {
                    if (checkList[1] === '2' || checkList[1] === '4') {
                      return data.map(item => item.amount2 / 100)
                    } else {
                      return data.map(item => item.amount2)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }
        } else if (type === 'month') {
          if (checkList.length === 1) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#2ec7c9', '#f4516c'],
              legend: {
                data: [name],
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: name,
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          } else if (checkList.length === 2) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#f4516c', '#2ec7c9'],
              legend: {
                data: nameArray,
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: [
                {
                  type: 'value',
                  // name: '销售额'
                },
                {
                  type: 'value',
                  // name: '客流量'
                }
              ],
              series: [
                {
                  name: nameArray[0],
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                },
                {
                  name: nameArray[1],
                  type: 'line',
                  yAxisIndex: 1,
                  data: (() => {
                    if (checkList[1] === '2' || checkList[1] === '4') {
                      return data.map(item => item.amount2 / 100)
                    } else {
                      return data.map(item => item.amount2)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }
        } else if (type === 'year') {
          if (checkList.length === 1) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#2ec7c9', '#f4516c'],
              legend: {
                data: [name],
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: name,
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          } else if (checkList.length === 2) {
            let options = {
              tooltip: {
                trigger: 'axis'
              },
              color: ['#f4516c', '#2ec7c9'],
              legend: {
                data: nameArray,
                // y: 'bottom'
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                data: (() => {
                  return data.map(item => {
                    let time = item.time.split(' ')[0]
                    time = `${time}`
                    return time
                  })
                })()
              },
              yAxis: [
                {
                  type: 'value',
                  // name: '销售额'
                },
                {
                  type: 'value',
                  // name: '客流量'
                }
              ],
              series: [
                {
                  name: nameArray[0],
                  type: 'line',
                  data: (() => {
                    if (checkList[0] === '2' || checkList[0] === '4') {
                      return data.map(item => item.amount1 / 100)
                    } else {
                      return data.map(item => item.amount1)
                    }
                  })()
                },
                {
                  name: nameArray[1],
                  type: 'line',
                  yAxisIndex: 1,
                  data: (() => {
                    if (checkList[1] === '2' || checkList[1] === '4') {
                      return data.map(item => item.amount2 / 100)
                    } else {
                      return data.map(item => item.amount2)
                    }
                  })()
                }
              ]
            }
            context.commit(types.SET_PEOPLE_NUMBER_ANALYZE_LINE_CHARTS, options)
          }
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 男女比例环形图
    async fetchSexPieData(context, payload) {
      let { startTime, endTime } = payload
      try {
        const { data } = await getSexPieData(startTime, endTime)
        let options = {
          color: ['#75bcff', '#f892cd'],
          series: [
            {
              type: 'pie',
              radius: ['50%', '70%'],
              hoverAnimation: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                }
              },
              itemStyle: {
                borderWidth: 2,
                borderColor: '#fff',
              },
              data: [
                { value: data.maleNumber },
                { value: data.femaleNumber }
              ]
            }
          ]
        }
        context.commit(types.SET_SEX_DATA, options)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 回头客分析环形图
    async fetchFrequencyAnalysisPieData(context, payload) {
      let { startTime, endTime } = payload
      try {
        const { data } = await getFrequencyAnalysisPieData(startTime, endTime)
        let options = {
          color: ['#facc15', '#3dc2c2', '#2990ff'],
          series: [
            {
              type: 'pie',
              radius: ['50%', '70%'],
              hoverAnimation: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                }
              },
              itemStyle: {
                borderWidth: 2,
                borderColor: '#fff',
              },
              data: [
                { value: data.new },
                { value: data.low },
                { value: data.high }
              ]
            }
          ]
        }
        context.commit(types.SET_FREQUENCY_ANALYSIS_DATA, options)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 客流分析表
    async fetchPeopleNumberData(context, payload) {
      let { type, startTime, endTime, perPage, page } = payload
      try {
        const { data } = await getPeopleNumberData(type, startTime, endTime, perPage, page)
        return data
      } catch (err) {
        console.log(err)
      }
    },

    // 抓拍记录
    async fetchCaptureRecordsData(context, payload) {
      let { startDate, endDate, perPage, page } = payload
      try {
        const { data } = await getCaptureRecordsData(startDate, endDate, perPage, page)
        return data
      } catch (err) {
        console.log(err)
      }
    },


    /**
     * 时段客流分析对比
     */
    // 对比图数据
    async fetchContrastChartsData(context, payload) {
      let { type, period, first, second } = payload
      try {
        const { data } = await getContrastChartsData(type, period, first, second)
        let firstLegendData = `${JSON.parse(first).from_date} ~ ${JSON.parse(first).to_date}`
        let secondLegendData = `${JSON.parse(second).from_date} ~ ${JSON.parse(second).to_date}`
        let legendData = []
        legendData.push(firstLegendData)
        legendData.push(secondLegendData)
        if (period === 'hour'){
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#2ec7c9', '#f4516c'],
            legend: {
              data: legendData,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                if (data.first.length > data.second.length){
                  return data.first.map(item => {
                    let hours = item.hours
                    hours = `${hours}:00`
                    return hours
                  })
                }else{
                  return data.second.map(item => {
                    let hours = item.hours
                    hours = `${hours}:00`
                    return hours
                  })
                }
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: firstLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3){
                    return data.first.map(item => item.amount)
                  } else if (type === 2 || type === 4){
                    return data.first.map(item => item.amount / 100)
                  }
                })()
              },
              {
                name: secondLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.second.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.second.map(item => item.amount / 100)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_CONTRAST_CHARTS_DATA, options)
        } else if (period === 'day'){
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#2ec7c9', '#f4516c'],
            legend: {
              data: legendData,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                if (data.first.length > data.second.length) {
                  return data.first.map(item => {
                    return item.reportDate
                  })
                } else {
                  return data.second.map(item => {
                    return item.reportDate
                  })
                }
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: firstLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.first.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.first.map(item => item.amount / 100)
                  }
                })()
              },
              {
                name: secondLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.second.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.second.map(item => item.amount / 100)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_CONTRAST_CHARTS_DATA, options)
        } else if (period === 'offset_week'){
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#2ec7c9', '#f4516c'],
            legend: {
              data: legendData,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                if (data.first.length > data.second.length) {
                  return data.first.map(item => {
                    return `第${item.week}周`
                  })
                } else {
                  return data.second.map(item => {
                    return `第${item.week}周`
                  })
                }
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: firstLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.first.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.first.map(item => item.amount / 100)
                  }
                })()
              },
              {
                name: secondLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.second.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.second.map(item => item.amount / 100)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_CONTRAST_CHARTS_DATA, options)
        } else if (period === 'offset_month') {
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#2ec7c9', '#f4516c'],
            legend: {
              data: legendData,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                if (data.first.length > data.second.length) {
                  return data.first.map(item => {
                    return `第${item.month}月`
                  })
                } else {
                  return data.second.map(item => {
                    return `第${item.month}月`
                  })
                }
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: firstLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.first.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.first.map(item => item.amount / 100)
                  }
                })()
              },
              {
                name: secondLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.second.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.second.map(item => item.amount / 100)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_CONTRAST_CHARTS_DATA, options)
        } else if (period === 'offset_year') {
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#2ec7c9', '#f4516c'],
            legend: {
              data: legendData,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                if (data.first.length > data.second.length) {
                  return data.first.map(item => {
                    return `第${item.year}年`
                  })
                } else {
                  return data.second.map(item => {
                    return `第${item.year}年`
                  })
                }
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: firstLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.first.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.first.map(item => item.amount / 100)
                  }
                })()
              },
              {
                name: secondLegendData,
                type: 'line',
                data: (() => {
                  if (type === 1 || type === 3) {
                    return data.second.map(item => item.amount)
                  } else if (type === 2 || type === 4) {
                    return data.second.map(item => item.amount / 100)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_CONTRAST_CHARTS_DATA, options)
        }
        return data
      } catch (err) {
        console.log(err)
      }
    },

    /**
    * 时段客流热力图
    */
    // 对比图数据
    async fetchRegionalChartsData(context, payload) {
      let { startDate, endDate } = payload

      try {
        const { data } = await getRegionalChartsData(startDate, endDate)
        console.log(data)
        var chartData = data.chart.list
        var chartTtile = ""
        if( chartData.length > 1){
          var max = chartData[1]
          chartTtile = "所有出入口中，" + max.region + "的客流量最大，共" + max.removelDuplicateCnt + "，占总出入口客流" + max.flowRate + "%"
        }

        var xAxis = []
        var mainSeries = []
        var subSeries = []
        var totalCnt = 0
        for (var i = 0; i < chartData.length; i++) {
          var cd = chartData[i]
          xAxis[i] = cd.region
          mainSeries[i] = cd.removelDuplicateCnt
          if (i == 0) {
            subSeries[i] = 0
          }else {
            totalCnt += cd.removelDuplicateCnt
            subSeries[i] = chartData[0].removelDuplicateCnt - totalCnt
            console.log(i, chartData[0].removelDuplicateCnt, totalCnt, cd.removelDuplicateCnt, subSeries[i])
          }
        }

        let options = {
          title: {
            text: chartTtile,
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter: function (params) {
                var tar = params[1];
                return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
            }
        },
        grid: {
            left: '3%',
            right: '3%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            splitLine: {show: false},
            data: xAxis
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '辅助',
                type: 'bar',
                stack: '总量',
                itemStyle: {
                    barBorderColor: 'rgba(0,0,0,0)',
                    color: 'rgba(0,0,0,0)'
                },
                emphasis: {
                    itemStyle: {
                        barBorderColor: 'rgba(0,0,0,0)',
                        color: 'rgba(0,0,0,0)'
                    }
                },
                data: subSeries
            },
            {
                name: '人流量',
                type: 'bar',
                stack: '总量',
                label: {
                    show: true,
                    position: 'inside'
                },
                data: mainSeries
            }
        ]
        }
        context.commit(types.SET_REG_CHARTS_DATA, options)
        return data
      } catch (err) {
        console.log(err)
      }
    },


    /**
     * 节假日活动分析
     */
    // 获取今年假日列表
    async fetchThisYearHolidayList(context, payload) {
      let { year } = payload
      try {
        const { data } = await getThisYearHolidayList(year)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 获取往年节假日列表
    async fetchFormerHolidayList(context, payload) {
      let { formerYear } = payload
      try {
        const { data } = await getFormerHolidayList(formerYear)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 节假日对比图
    async fetchHolidayChartsData(context, payload) {
      let { checkList, period, first, second } = payload
      try {
        const { data } = await getHolidayChartsData(checkList, period, first, second)
        // 1.元旦 2.春节 3.清明节 4.劳动节 5.端午节 6.中秋节 7.国庆节
        let codeArray = ['元旦', '春节', '清明节', '劳动节', '端午节', '中秋节', '国庆节']

        let firstData = (JSON.parse(first))
        let filterFirstCodeName = codeArray.map((item, index) => ({ code: index + 1, codeName: item }))
        let firstObj = {}
        firstObj = filterFirstCodeName.find((item) => {
          return item.code === firstData.code / 1
        })
        let firstCodeName = firstObj.codeName

        let secondData = (JSON.parse(second))
        let filterSecondCodeName = codeArray.map((item, index) => ({ code: index + 1, codeName: item }))
        let secondObj = {}
        secondObj = filterSecondCodeName.find((item) => {
          return item.code === secondData.code / 1
        })
        let secondCodeName = secondObj.codeName

        let newName = [] // 1. 去重客流 2.销售额(元) 3.订单数 4.客单价(元)
        let nameArray = [] // 1. 去重客流 2.销售额(元) 3.订单数 4.客单价(元)
        let newCheckList = (JSON.parse(checkList))
        if (newCheckList.length === 1) {
          if (newCheckList[0] === '1') {
            nameArray = [`${firstCodeName}去重客流`, `${secondCodeName}去重客流`]
          } else if (newCheckList[0] === '2') {
            nameArray = [`${firstCodeName}销售额(元)`, `${secondCodeName}销售额(元)`]
          } else if (newCheckList[0] === '3') {
            nameArray = [`${firstCodeName}订单数`, `${secondCodeName}订单数`]
          } else if (newCheckList[0] === '4') {
            nameArray = [`${firstCodeName}客单价(元)`, `${secondCodeName}客单价(元)`]
          }
        } else if (newCheckList.length === 2) {
          if (newCheckList[0] === '1') {
            if (newCheckList[1] === '2') {
              nameArray = [`${firstCodeName}去重客流`, `${secondCodeName}去重客流`, `${firstCodeName}销售额(元)`, `${secondCodeName}销售额(元)`]
              newName = ['去重客流', '销售额(元)']
            } else if (newCheckList[1] === '3') {
              nameArray = [`${firstCodeName}去重客流`, `${secondCodeName}去重客流`, `${firstCodeName}订单数`, `${secondCodeName}订单数`]
              newName = ['去重客流', '订单数']
            } else if (newCheckList[1] === '4') {
              nameArray = [`${firstCodeName}去重客流`, `${secondCodeName}去重客流`, `${firstCodeName}客单价(元)`, `${secondCodeName}客单价(元)`]
              newName = ['去重客流', '客单价(元)']
            }
          } else if (newCheckList[0] === '2') {
            if (checkList[1] === '3') {
              nameArray = [`${firstCodeName}销售额(元)`, `${secondCodeName}销售额(元)`, `${firstCodeName}订单数`, `${secondCodeName}订单数`]
              newName = ['销售额(元)', '订单数']
            } else if (newCheckList[1] === '4') {
              nameArray = [`${firstCodeName}销售额(元)`, `${secondCodeName}销售额(元)`, `${firstCodeName}客单价(元)`, `${secondCodeName}客单价(元)`]
              newName = ['销售额(元)', '客单价(元)']
            }
          } else if (newCheckList[0] === '3') {
            if (newCheckList[1] === '4') {
              nameArray = [`${firstCodeName}订单数`, `${secondCodeName}订单数`, `${firstCodeName}客单价(元)`, `${secondCodeName}客单价(元)`]
              newName = ['订单数', '客单价(元)']
            }
          }
        }
        if (newCheckList.length === 1) {
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#f4516c', '#2ec7c9'],
            legend: {
              data: nameArray,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: (() => {
                return data.first.map(item => {
                  return item.reportDate
                })
              })()
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: nameArray[0],
                type: 'line',
                data: (() => {
                  if (newCheckList[0] === '2' || newCheckList[0] === '4') {
                    return data.first.map(item => item.amount1 / 100)
                  } else {
                    return data.first.map(item => item.amount1)
                  }
                })()
              },
              {
                name: nameArray[1],
                type: 'line',
                data: (() => {
                  if (newCheckList[0] === '2' || newCheckList[0] === '4') {
                    return data.second.map(item => item.amount1 / 100)
                  } else {
                    return data.second.map(item => item.amount1)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_HOLIDAY_CHARTS_DATA, options)
        } else if (newCheckList.length === 2) {
          let options = {
            tooltip: {
              trigger: 'axis'
            },
            color: ['#f4516c', '#2ec7c9', '#facc15', '#fb9867'],
            legend: {
              data: nameArray,
              // y: 'bottom'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              // boundaryGap: false,  // 图形往内缩进
              data: (() => {
                if (data.first.length > data.second.length) {
                  return data.first.map(item => {
                    return item.reportDate
                  })
                } else {
                  return data.second.map(item => {
                    return item.reportDate
                  })
                }
              })(),
              axisPointer: {
                type: 'shadow'
              }
            },
            yAxis: [
              {
                type: 'value',
                name: newName[0]
              },
              {
                type: 'value',
                name: newName[1]
              }
            ],
            series: [
              {
                name: nameArray[0],
                type: 'line',
                data: (() => {
                  if (newCheckList[0] === '2' || newCheckList[0] === '4') {
                    return data.first.map(item => item.amount1 / 100)
                  } else {
                    return data.first.map(item => item.amount1)
                  }
                })()
              },
              {
                name: nameArray[2],
                type: 'line',
                data: (() => {
                  if (newCheckList[1] === '2' || newCheckList[1] === '4') {
                    return data.first.map(item => item.amount2 / 100)
                  } else {
                    return data.first.map(item => item.amount2)
                  }
                })()
              },
              {
                name: nameArray[1],
                type: 'line',
                yAxisIndex: 1,
                data: (() => {
                  if (newCheckList[0] === '2' || newCheckList[0] === '4') {
                    return data.second.map(item => item.amount1 / 100)
                  } else {
                    return data.second.map(item => item.amount1)
                  }
                })()
              },
              {
                name: nameArray[3],
                type: 'line',
                yAxisIndex: 1,
                data: (() => {
                  if (newCheckList[1] === '2' || newCheckList[1] === '4') {
                    return data.second.map(item => item.amount2 / 100)
                  } else {
                    return data.second.map(item => item.amount2)
                  }
                })()
              }
            ]
          }
          context.commit(types.SET_HOLIDAY_CHARTS_DATA, options)
        }
        return data
      } catch (err) {
        console.log(err)
      }
    },


    /**
     * 回头客
     */
    // 获取 回头客概况
    async fetchFrequentSummary(context, payload) {
      let { date } = payload
      try {
        const { data } = await getFrequentSummary(date)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 回访活跃度分析 上次到访间隔天数分布
    async fetchFrequentActivities(context, payload) {
      try {
        const { data } = await getFrequentActivities(payload)
        // 上次到访间隔天数分布
        let optionsInterval = {
          color: ['#a1c4fe', '#f1c23f', '#fb9867', '#8adac0'],
          series: [
            {
              type: 'pie',
              hoverAnimation: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                }
              },
              itemStyle: {
                borderWidth: 2,
                borderColor: '#fff',
              },
              data: [
                { value: data.interval[0].count},
                { value: data.interval[1].count},
                { value: data.interval[2].count},
                { value: data.interval[3].count}
              ]
            }
          ]
        }
        context.commit(types.SET_LAST_TIME_DATA, optionsInterval)
        // 近一月到访天数分布
        let optionsFrequency = {
          color: ['#a1c4fe', '#f1c23f', '#fb9867', '#8adac0', '#f6a5c8'],
          series: [
            {
              type: 'pie',
              hoverAnimation: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                }
              },
              itemStyle: {
                borderWidth: 2,
                borderColor: '#fff',
              },
              data: [
                { value: data.frequency[0].count },
                { value: data.frequency[1].count },
                { value: data.frequency[2].count },
                { value: data.frequency[3].count },
                { value: data.frequency[4].count }
              ]
            }
          ]
        }
        context.commit(types.SET_IN_RECENT_MONTH_DATA, optionsFrequency)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 近一周高频回头客来访时段分布
    async fetchG2AntvFrequentTable(context, payload) {
      let { date } = payload
      try {
        const { data } = await getG2AntvFrequentTable(date)
        return data
      } catch (err) {
        console.log(err)
      }
    },
    // 回头客/新客分布走势分析
    async fetchFrequencyDistributions(context, payload) {
      let { date, type } = payload
      try {
        const { data } = await getFrequencyDistributions(date,type)
        const newLineChartDate = []
        for (let i = 0; i < data.lineChart.length; i++) {
          const date = data.lineChart[i].date
          newLineChartDate.push(date)
        }
        let options = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          color: ['#8adac0', '#f1c23f', '#a1c4fe', '#f6a5c8', '#fb9867'],
          legend: {
            data: ['新客', '低频回头客', '高频回头客']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: newLineChartDate
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '新客',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: (() => {
                return data.lineChart.map(item => item.new)
              })()
            },
            {
              name: '低频回头客',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: data.lineChart.map(item => item.low)
            },
            {
              name: '高频回头客',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: data.lineChart.map(item => item.high)
            }
          ]
        }
        context.commit(types.SET_FREQUENCY_DISTRIBUTIONS_DATA, options)
        return data
      } catch (err) {
        console.log(err)
      }
    }
  }
}
